





function Task(name) {
    this.name = name;
    //任务状态 ：未开始 成功  进行中  失败  无法执行  
    this.state = "未开始";
    //入口按钮
    this.enterbtn = null;
}

let openBox = new Task("开宝箱")
let lookAD = new Task("看广告")
let lookProduct = new Task("逛街浏览商品")
let lookzbj = new Task("看直播间")
let lookvd = new Task("刷短视频")
let findLook = new Task("搜素浏览")
let eatTime = new Task("每日餐补")
let sleepgetup = new Task("早睡早起")
let getCoinNum = new Task("获取金币数量")
let looktv = new Task("看短剧")
let readNovel = new Task("爽读小说")
let toOpenApp = new Task("打开app")
let lokzbj2 = new Task("去金币购赚更多")

lookAD.running = function () {
    log(`\n【${this.name}任务】`)

    this.enterbtn = className("android.widget.TextView").textMatches(/看广告得.+|看视频得.+/).findOne(2000)
    if (this.enterbtn) {
        let state1 = this.enterbtn.parent().findOne(textMatches(/.+(\d+)\/(\d+)/)) //已经完成 2/6
        if (state1) {
            let num = state1.text().match(/(\d+)\/(\d+)/)
            if (num[1] == num[2]) {
                log(`已完成:${num[1]}/${num[2]}`)
                this.state = "成功"
                return;
            }
        }

        if (this.enterbtn.parent().findOne(textStartsWith("明天看广告"))) {
            this.state = "成功"
            return;
        }

        sleep(2000)
        log(`【找到[${this.enterbtn.text()}]并点击】:${tools.superClick(this.enterbtn)}`)
        sleep(4000)
        //等待
        if (!tools.waitforUi(text("任务中心"), 30, () => sleep(10000))) { return }

    } else {
        log("未找到广告任务入口")
        this.state = "无法执行"
    }

}
openBox.running = function () {
    log(`\n【${this.name}任务】`)

    this.enterbtn = textMatches(/treasurebox|开宝箱得金币|点可领.+|点就领.+/).findOne(2000)
    if (this.enterbtn) {
        click(this.enterbtn.bounds().centerX(), this.enterbtn.bounds().centerY());
        if (!tools.waitforUi(text("任务中心"), 20, () => sleep(10000))) { return }


    } else {
        log("暂无宝箱")
    }

}
lookProduct.running = function () {
    log(`\n【${this.name}任务】`)

    this.enterbtn = className("android.widget.TextView").text("逛街领金币").findOne(2000)
    if (this.enterbtn) {
        if (this.enterbtn.parent().findOne(textStartsWith("今日福利已领取"))) {
            log("逛街领金币已完成")
            this.state = "成功"
            return;
        }

        log(`【找到[${this.enterbtn.text()}]并点击】:${tools.superClick(this.enterbtn)}`)
        sleep(3000)
        if (!tools.waitforUi(text("任务中心"), 13, () => {
            tools.randomSwipe(w, h, "上", 2000, 3200);
            tools.randomSwipe(w, h, "上", 2000, 3200)
            tools.randomSwipe(w, h, "下", 2000, 3200)
        })) { return }


    } else {
        this.state = "无法执行"
        log("未找到入口")
    }
}

lookzbj.running = function () {
    log(`\n【${this.name}任务】`)

    this.enterbtn = className("android.widget.TextView").textMatches(/.+次直播领金币/).findOne(2000)
    if (this.enterbtn) {
        let num = [0, 0]
        let state1 = this.enterbtn.parent().findOne(textMatches(/.+(\d+)\/(\d+)/)) //已经完成 2/6
        if (state1) {
            num = state1.text().match(/(\d+)\/(\d+)/)
            if (num[1] == num[2]) {
                log(`已完成:${num[1]}/${num[2]}`)
                this.state = "成功"
                return;
            }
        }
        if (this.enterbtn.parent().findOne(text("今日已成功领取直播奖励金币"))) {
            this.state = "成功"
            return;
        }


        log(`【找到[${this.enterbtn.text()}]并点击】:${tools.superClick(this.enterbtn)}`)

        sleep(3000)
        tools.randomSwipe(w, h, "下", 0, 0)
        tools.randomSwipe(w, h, "下", 1000, 2000)
        sleep(1000)
        let maxcount = num[2] - num[1]
        for (let i = 0; i < maxcount; i++) {
            let zbj = id("recycler_view").findOne(2000)
            if (zbj && i < zbj.childCount()) {
                sleep(1000)
                log(`第${i + 1}/${maxcount}次进入直播间`)
                tools.superClick(zbj.child(i))
                if (!tools.waitforUi(text("看直播领金币"), 30, () => sleep(10000))) { return }
            } else {
                log("未找到直播间控件/已看完")
                break;
            }
        }
    } else {
        log("未找到入口")
        this.state = "无法执行"
    }
}

lookvd.running = function () {
    log(`\n【${this.name}任务】`)

    sleep(2000)
    if (textMatches(/今日已领.+明日可领.+金币/).exists()) {
        log("刷视频任务已完成")
        this.state = "成功"
        return;
    }

    this.enterbtn = textMatches(/看视频额外得\d+金币|看视频赚金币|首页/).findOne(2000)
    if (this.enterbtn) {

        log(`【找到[${this.enterbtn.text()}]并点击】:${tools.superClick(this.enterbtn)}`)
        sleep(2000)

        let i = 0;
        while (i < 20) {
            log(`浏览视频第 ${i}/2020次`)
            tools.randomSwipe(w, h, "下", 5000, 8000)
            tools.randomSwipe(w, h, "下", 6000, 7000)
            tools.randomSwipe(w, h, "上", 8000, 10000)

            i++;
        }

        //回到任务中心
        if (tools.backTo(id("android:id/text1").text("首页"), 3)) {
            let taskcenter = id("float_background").findOne(2000) || className("android.widget.CheckedTextView").text("去赚钱").findOne(2000)
            if (taskcenter) {
                log(`【找到[${taskcenter.text()}]并点击】:${tools.superClick(taskcenter)}`)
                sleep(4000);
            } else {
                log("未找到 任务中心")
            }
        }



    } else {
        log("未找到入口")
        this.state = "无法执行"
    }
}

findLook.running = function () {
    log(`\n【${this.name}任务】`)

    this.enterbtn = className("android.widget.TextView").textMatches(/搜索.+/).findOne(2000)
    if (this.enterbtn) {
        let state1 = this.enterbtn.parent().findOne(textMatches(/.+(\d+)\/(\d+)/)) //已经完成 2/6
        if (state1) {
            let num = state1.text().match(/(\d+)\/(\d+)/)
            if (num[1] == num[2]) {
                log(`已完成:${num[1]}/${num[2]}`)
                this.state = "成功"
                return;
            }
        }

        //要输入的内容
        let context = this.enterbtn.text().match(/“(.+?)”/) || this.enterbtn.text().match(/\"(.+?)\"/)
        log(`【找到[${this.enterbtn.text()}]并点击】:${tools.superClick(this.enterbtn)}`)
        log(context)

        sleep(2000)
        let state = text("任务中心").findOne(2000)
        if (state) {
            log(`倒计时结束后可看`)
            return;
        }


        let btn = className("android.widget.TextView").text("搜索").findOne(2000)
        if (btn && context) {
            log("输入：" + context[1])
            setText(context[1])
            sleep(1000)
            log(`【找到[${btn.text()}]并点击】:${tools.superClick(btn)}`)
            sleep(22000)
            back()
        } else {
            log("未找到搜索按钮")
        }


        //回到任务中心
        if (tools.backTo(id("android:id/text1").text("首页"), 3)) {
            let taskcenter = id("float_background").findOne(2000) || className("android.widget.CheckedTextView").text("去赚钱").findOne(2000)
            if (taskcenter) {
                log(`【找到[${taskcenter.text()}]并点击】:${tools.superClick(taskcenter)}`)
                sleep(4000);
            } else {
                log("未找到 任务中心")
            }
        }

    } else {
        log("未找到入口")
        this.state = "无法执行"
    }
}

eatTime.running = function () {
    log(`\n【${this.name}任务】`)

    // 获取当前北京时间的时间戳（单位为毫秒）
    var stamp = new Date().getTime() + 8 * 60 * 60 * 1000;
    // 格式化北京时间为"YYYY-MM-DD HH:mm:ss"
    //replace() -->字符串分割  substring(0, 10); -->取 年月日 
    var beijingTime = new Date(stamp).toISOString().replace(/T/, ' ').replace(/\..+/, '').substring(0, 10);
    //当前小时    Date.parse( )-->转化成时间戳     向下取整
    let date = Math.floor((stamp - Date.parse(beijingTime)) / (60 * 60 * 1000))
    switch (date) {
        case 1:
        case 2:
        case 3:
        case 4:
        case 9:
        case 10:
        case 14:
        case 20:
        case 0:
            log(`未到餐补时间--》目前：${date}点`)
            this.state = "无法执行"
            break
        default:
            log(`餐补时间--》目前：${date}点`)
            this.enterbtn = className("android.widget.TextView").text("到饭点领饭补").findOne(2000)
            if (this.enterbtn) {
                log(`【找到[${this.enterbtn.text()}]并点击】:${tools.superClick(this.enterbtn)}`)
                sleep(3000)
                //点击领取
                let next1 = text("领取饭补").findOne(2000) || textMatches(/(.+?)领取(.+?)补贴/).findOne(2000)
                if (next1) {
                    log(`【找到[${next1.text()}]并点击】:${tools.superClick(next1)}`)
                    this.state = "成功"
                } else {
                    log("未找到 领取控件")
                }

                sleep(2000)
                let next2 = textEndsWith("待补签").find()
                if (next2) {
                    for (let i = 0; i < next2.length; i++) {
                        log(`【第${i}次找到[${next2[i].text()}]并点击】:${tools.superClick(next2[i])}`)
                        sleep(40000)
                    }
                } else {
                    log("未找到 补签控件")
                }

                return;

            } else {
                log("未找到饭补入口");
                this.state = "失败"
            }





    }
}
sleepgetup.running = function () {
    log(`\n【${this.name}任务】`)

    // 获取当前北京时间的时间戳（单位为毫秒）
    var stamp = new Date().getTime() + 8 * 60 * 60 * 1000;
    // 格式化北京时间为"YYYY-MM-DD HH:mm:ss"
    //replace() -->字符串分割  substring(0, 10); -->取 年月日 
    var beijingTime = new Date(stamp).toISOString().replace(/T/, ' ').replace(/\..+/, '').substring(0, 10);
    //当前小时    Date.parse( )-->转化成时间戳     向下取整
    let date = Math.floor((stamp - Date.parse(beijingTime)) / (60 * 60 * 1000))
    switch (date) {
        case 5:
        case 6:
        case 7:
        case 8:
        case 9:
            log(`起床时间--》目前：${date}点`)
            this.enterbtn = className("android.widget.TextView").textMatches(/起床赚金币|睡觉赚金币/).findOne(2000)
            if (this.enterbtn) {
                sleep(2000)
                if (text("已起床").exists()) {
                    log("已起床")
                    this.state = "成功"
                    return;
                }
                log(`【找到[${this.enterbtn.text()}]并点击】:${tools.superClick(this.enterbtn)}`)
                sleep(2000)
                //点击领取
                let next = text("开始起床").findOne(2000)
                if (next) {
                    log(`【找到[${next.text()}]并点击】:${tools.superClick(next)}`)
                } else {
                    log("未找到 领取控件")
                }
            } else {
                log("未找到入口");
                this.state = "失败"

            }
            break
        case 20:
        case 21:
        case 22:
        case 23:
            log(`睡觉时间--》目前：${date}点`)
            this.enterbtn = className("android.widget.TextView").textMatches(/起床赚金币|睡觉赚金币/).findOne(2000)
            if (this.enterbtn) {
                sleep(2000)
                if (text("已睡觉").exists()) {
                    log("已睡觉")
                    this.state = "成功"
                    return;
                }
                sleep(2000)
                log(`【找到[${this.enterbtn.text()}]并点击】:${tools.superClick(this.enterbtn)}`)
                //点击领取
                let next = text("开始睡觉").findOne(2000)
                if (next) {
                    log(`【找到[${next.text()}]并点击】:${tools.superClick(next)}`)
                } else {
                    log("未找到 领取控件")
                }
            } else {
                log("未找到入口");
                this.state = "无法执行"
            }
            break
        default:
            log("时间未到")
            this.state = "无法执行"

    }

}

getCoinNum.begin_coinNum = 0
getCoinNum.end_coinNum = 0
getCoinNum.running = function () {
    log(`\n【${this.name}任务】`)
    this.enterbtn = className("android.widget.TextView").text("我的金币").findOne(2000)
    if (this.enterbtn) {
        log(`【找到[${this.enterbtn.text()}]并点击】:${tools.superClick(this.enterbtn)}`)
        sleep(4000)
        if (text("我的金币").exists()) {
            let myCoinText = text("我的金币").findOne(2000);
            if (myCoinText) {
                let myCoinBounds = myCoinText.bounds();
                // 在"我的金币"文本下方查找纯数字（包括小数点和w后缀）的文本
                let coin = textMatches(/(\d*\.?\d+w?)/)
                            .boundsInside(myCoinBounds.left, myCoinBounds.bottom, myCoinBounds.right, myCoinBounds.bottom + 100)
                            .findOne(2000);
                if (coin) {
                    if (this.state == "未开始") {
                        this.begin_coinNum = this.end_coinNum = coin.text().match(/(\d*\.?\d+)/)[0];
                        this.begin_coinNum.toString().search(/\./) != -1 && (this.begin_coinNum = Math.ceil(this.begin_coinNum * 10000));
                    } else {
                        this.end_coinNum = coin.text().match(/(\d*\.?\d+)/)[0];
                        this.end_coinNum.toString().search(/\./) != -1 && (this.end_coinNum = Math.ceil(this.end_coinNum * 10000));
                    }
                    log("this.begin_coinNum-->" + this.begin_coinNum, "this.end_coinNum-->" + this.end_coinNum);
                    this.state = "进行中";
                } else {
                    log("未找到金币数字");
                }
            } else {
                log("未找到我的金币文本");
            }
        } else {
            log("我的金币不存在");
        }

    } else {
        log("未找到 -- 我的金币")
    }

}

looktv.running = function () {

    log(`\n【${this.name}任务】`)

    this.enterbtn = className("android.widget.TextView").textMatches(/看短剧得.+金币/).findOne(2000)
    if (this.enterbtn) {
        let state1 = this.enterbtn.parent().findOne(textMatches(/.+(\d+)\/(\d+)/)) //已经完成 2/6
        if (state1) {
            let num = state1.text().match(/(\d+)\/(\d+)/)
            if (num[1] == num[2]) {
                log(`已完成:${num[1]}/${num[2]}`)
                this.state = "成功"
                return;
            }
        }



        log(`【找到[${this.enterbtn.text()}]并点击】:${tools.superClick(this.enterbtn)}`)
        sleep(2500)
        //等待
        if (!tools.waitforUi(text("任务中心"), 6, () => sleep(10000))) { return }

    } else {
        log("未找到广告任务入口")
        this.state = "无法执行"
    }


}
readNovel.running = function () {
    log(`\n【${this.name}任务】`)

    this.enterbtn = className("android.widget.TextView").text("爽读小说咔咔赚金币").findOne(2000)
    if (this.enterbtn) {

        log(`【找到[${this.enterbtn.text()}]并点击】:${tools.superClick(this.enterbtn)}`)
        sleep(10000)
        let book = className("android.view.View").textMatches(/.*每读\(\d+\/\d+\)(分|章)/).findOne(2000)
        if (book) {
            sleep(4000)
            tools.randomSwipe(w, h, "右", 0, 0)
            tools.randomSwipe(w, h, "右", 500, 1000)
            tools.randomSwipe(w, h, "右", 500, 1000)
            log(`【找到[${book.text()}]并点击】:${tools.superClick(book)}`)
            sleep(1400)
            if (!
                tools.waitforUi(text("任务中心"), 130, () => {
                    tools.randomSwipe(w, h, "右", 2500, 5200)
                })) { return }


        } else {
            tools.backTo(text("任务中心"), 3)
        }


    } else {
        this.state = "无法执行"
        log("未找到入口")
    }
}

toOpenApp.running = function () {
    log(`\n【${this.name}任务】`)
    if (this.state == "成功") {
        return;
    }
    this.state = "成功"
    let app_arr = ["快影", "淘宝", "一淘", "点淘"]
    for (let i = 0; i < app_arr.length; i++) {
        let appbtn = className("android.widget.TextView").textContains(app_arr[i]).findOne(2000)
        if (appbtn) {
            if (appbtn.text().search(/登录|打开|去/) == 0) {
                if (appbtn.text().search(/施肥/) != -1) {
                    //去施肥 ，如果是小米 小窗开启的坐标为(540 1160) 
                    // tools.clicks(540, 1940)
                    continue;
                }
                if (appbtn.parent().parent().findOne(text("已完成"))) {
                    log(`打开${app_arr[i]}任务 已完成`)
                    continue;
                } else {

                    log("任务开始：打开" + app_arr[i])
                    if (tools.superClick(appbtn)) {
                        sleep(10000)
                        tools.killApp(app_arr[i])
                        sleep(2000)
                        launchApp(appname)
                        if (!tools.backTo(text("任务中心"), 4)) { return }
                    }
                }

            }
        } else {
            log(`打开${app_arr[i]}任务未找到`)
        }

    }
}

lokzbj2.running = function () {
    log(`\n【${this.name}任务】`)

    this.enterbtn = className("android.widget.TextView").text("去金币购赚更多金币").findOne(2000)
    if (this.enterbtn) {
        log(`【找到[${this.enterbtn.text()}]并点击】:${tools.superClick(this.enterbtn)}`)
        sleep(4000)
        let btn = textContains("今日签到").findOne(2000)
        if (btn) {
            log(`【找到[${btn.text()}]并点击】:${tools.superClick(btn)}`)
        }
        sleep(1400)
        let btn1 = textContains("看直播可领").findOne(2000)
        if (btn1) {
            if (btn1.text() == "明日看直播可领") {
                log("任务已完成")
                this.state = "成功"
                return;
            }
            log(`【找到[${btn1.text()}]并点击】:${tools.superClick(btn1)}`)
            sleep(4000)
            let a = text("直播间下单还能返金币").findOne(2000)
            if (a) {
                let zbjs = a.parent().parent().child(2)
                for (let i = 0; i < 3; i++) {
                    if (zbjs && i < zbjs.childCount()) {
                        log(`第${i + 1}/3次进入直播间`)
                        zbjs.child(i).click()
                        if (!tools.waitforUi(text("直播间下单还能返金币"), 11, () => sleep(10000))) { return }
                    } else {
                        log("未找到直播间控件/已看完")
                        this.state = "成功"
                        return;
                    }
                }
            } else {
                log("未找到控件")
            }
        }

    } else {
        log("未找到入口")
        this.state = "无法执行"
    }
}


// 爽读小说咔咔赚金币
var tasksarr = [getCoinNum, openBox, lookAD, sleepgetup, eatTime, lookProduct, looktv, lokzbj2, getCoinNum, toOpenApp, lookzbj, findLook, lookvd]

//暴露模块
module.exports = tasksarr;



    